package server.business.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import server.business.mapper.BloodOutboundMapper;
import server.business.pojo.BloodOutbound;
import server.business.service.BloodOutboundService;
import server.business.vo.ResultExpand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
* @author Galaxy
* @description 针对表【blood_outbound】的数据库操作Service实现
* @createDate 2022-08-03 13:36:56
*/
@Service("bloodOutboundService")
public class BloodOutboundServiceImpl extends ServiceImpl<BloodOutboundMapper, BloodOutbound> implements BloodOutboundService {
    
    @Autowired
    private BloodOutboundMapper bloodOutboundMapper;
    @Autowired
    private BloodOutboundService bloodOutboundService;

    public IPage<BloodOutbound> selectByPage(int currentPage, int pageSize){
        Page<BloodOutbound> page = new Page<>(currentPage,pageSize);
        IPage<BloodOutbound> bloodOutboundIPage = bloodOutboundMapper.selectPage(page,null);
        return bloodOutboundIPage;
    }

    public ResultExpand searchBloodOutBound(HttpServletRequest request, HttpServletResponse response) {
        //接收前端获取的参数
        String outboundNumber = request.getParameter("outboundNumber");
        String outboundReason = request.getParameter("outboundReason");
        String outboundLocation = request.getParameter("outboundLocation");
        String operator = request.getParameter("operator");
        String operatorNumber = request.getParameter("operatorNumber");
        String bloodTaker = request.getParameter("bloodTaker");
        String bloodTakerNumber = request.getParameter("bloodTakerNumber");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //创建分页对象
        IPage<BloodOutbound> page = new Page<>(currentPage, pageSize);
        //创建查询条件
        QueryWrapper<BloodOutbound> queryWrapper = new QueryWrapper<>();
        if (outboundNumber != "")
            queryWrapper.like("outbound_number", outboundNumber);
        if (outboundReason != "")
            queryWrapper.eq("outbound_reason", outboundReason);
        if (outboundLocation != "")
            queryWrapper.eq("outbound_location", outboundLocation);
        if (operator != "")
            queryWrapper.eq("operator", operator);
        if (operatorNumber != "")
            queryWrapper.eq("operator_number", operatorNumber);
        if (bloodTaker != "")
            queryWrapper.eq("blood_taker", bloodTaker);
        if (bloodTakerNumber != "")
            queryWrapper.eq("blood_taker_number", bloodTakerNumber);
        if (startTime != "")
            queryWrapper.ge("outbound_time", startTime);
        if (endTime != "")
            queryWrapper.le("outbound_time", endTime + " 23:59:59");
        //查询数据库
        bloodOutboundService.page(page, queryWrapper);
//        IPage<BloodInBoundData> page = bloodInBoundMapper.selectInboundpage(
//                new Page<>(currentPage, pageSize),  queryWrapper
//        );
        //返回的数据
        ResultExpand resultExpand = new ResultExpand();
        resultExpand.setCurrent(currentPage).setPageSize(pageSize).setTotal(page.getTotal()).setPages(page.getPages());
        resultExpand.setCode("200");
        resultExpand.setMsg("搜索成功");
        resultExpand.setData(page.getRecords());
        System.out.println(page.getRecords());
        return resultExpand;
    }

    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) {
        //接收前端获取的参数
        String outboundNumber = request.getParameter("outboundNumber");
        String outboundReason = request.getParameter("outboundReason");
        String outboundLocation = request.getParameter("outboundLocation");
        String operator = request.getParameter("operator");
        String operatorNumber = request.getParameter("operatorNumber");
        String bloodTaker = request.getParameter("bloodTaker");
        String bloodTakerNumber = request.getParameter("bloodTakerNumber");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        try {
            String filename = "出库信息";
            String userAgent = request.getHeader("User-Agent");
            if(userAgent.contains("MSIE")||userAgent.contains("Trident")){
                filename = URLEncoder.encode(filename,"UTF-8");            
            }else {
                filename = new String(filename.getBytes("UTF-8"),"ISO-8859-1");
            }
            response.setContentType("application/json.ms-exce");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition","filename = " + filename + ".xlsx");
            //创建查询条件
            QueryWrapper<BloodOutbound> queryWrapper = new QueryWrapper<>();
            if (outboundNumber != "")
                queryWrapper.like("outbound_number", outboundNumber);
            if (outboundReason != "")
                queryWrapper.eq("outbound_reason", outboundReason);
            if (outboundLocation != "")
                queryWrapper.eq("outbound_location", outboundLocation);
            if (operator != "")
                queryWrapper.eq("operator", operator);
            if (operatorNumber != "")
                queryWrapper.eq("operator_number", operatorNumber);
            if (bloodTaker != "")
                queryWrapper.eq("blood_taker", bloodTaker);
            if (bloodTakerNumber != "")
                queryWrapper.eq("blood_taker_number", bloodTakerNumber);
            if (startTime != "")
                queryWrapper.ge("outbound_time", startTime);
            if (endTime != "")
                queryWrapper.le("outbound_time", endTime + " 23:59:59");
            List<BloodOutbound> bloodOutboundList = bloodOutboundService.list(queryWrapper);
            EasyExcel.write(response.getOutputStream(), BloodOutbound.class).sheet("sheet").doWrite(bloodOutboundList);
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    /**
     * fzx
     * @param startTime
     * @param endTime
     * @return
     */
    public List<Map<String, Object>> getBloodOutReportList(Timestamp startTime, Timestamp endTime) {
        QueryWrapper<BloodOutbound> wrapperOut = new QueryWrapper<>();
        wrapperOut
                .lambda()
                .select(BloodOutbound::getBloodId)
                .between(BloodOutbound::getOutboundTime, startTime, endTime);
        List<Map<String, Object>> listOutId = this.listMaps(wrapperOut);
        return listOutId;
    }
    
}




